<template>
  <div>
    <h1>图书库存查询</h1>
    <el-button type="primary" @click="addBook">添加图书</el-button>
    <el-table
      :data="bookList"
      style="width: 100%">
      <el-table-column
        prop="title"
        label="书名">
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者">
      </el-table-column>
      <el-table-column
        prop="quantity"
        label="库存数量">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="添加图书"
      :visible.sync="dialogVisible">
      <el-form :model="newBook" label-width="80px">
        <el-form-item label="书名">
          <el-input v-model="newBook.title"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="newBook.author"></el-input>
        </el-form-item>
        <el-form-item label="库存数量">
          <el-input v-model.number="newBook.quantity" type="number"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="newBook.status"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveNewBook">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bookList: [
        { id: 1, title: '环境保护与治理', author: '鼎真', quantity: 10, status: '在架上' },
        { id: 2, title: '我的动物朋友', author: '小马珍珠', quantity: 15, status: '借出' },
        { id: 3, title: 'Python编程从入门到精通', author: '了小孝钏', quantity: 20, status: '在架上' }
      ],
      dialogVisible: false,
      newBook: {
        title: '',
        author: '',
        quantity: 0,
        status: ''
      }
    };
  },
  methods: {
    handleEdit(index, row) {
      this.editingBook = { ...row };
      this.editingIndex = index;
      this.editingBook = { ...row };
      this.dialogVisible = true;
      
    },
    handleDelete(index, row) {
      this.$confirm('确认删除该图书吗？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {

    this.bookList.splice(index, 1);

    this.$message({
      type: 'success',
      message: '删除成功!'
    });
  }).catch(() => {
  });
    },
    addBook() {
      this.dialogVisible = true;
    },
    saveNewBook() {
      this.newBook.id = this.bookList.length + 1; // 生成新图书的ID
      this.bookList.push({ ...this.newBook }); // 将新图书添加到列表
      this.dialogVisible = false; // 关闭对话框
      // 清空新图书对象，以备下次添加
      this.newBook.title = '';
      this.newBook.author = '';
      this.newBook.quantity = 0;
      this.newBook.status = '';
    }
  }
};
</script>

<style scoped>

</style>
